package com.example.orderfoodsystem22201310liuziming.controller;

import com.example.orderfoodsystem22201310liuziming.entity.foodmenu;
import com.example.orderfoodsystem22201310liuziming.entity.shoppingorder;
import com.example.orderfoodsystem22201310liuziming.repository.MenuRepository;
import com.example.orderfoodsystem22201310liuziming.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/buyer/")
public class BuyerController {
    @Autowired //接线
    private MenuRepository menuRepository;
    @Autowired
    private HttpSession httpSession;
    @Autowired
    private OrderRepository orderRepository;
    @Value("${dishphoto.url}")
    private String dishUrl;

    @RequestMapping("/") //买家主页
    protected String buyerindex(){
        return "buyer/BuyerIndex";
    }

    @RequestMapping("/menulist") //买家菜单
    protected String menuList(Model model){
        if(null == httpSession.getAttribute("aaaa")) {
            String x = String.valueOf(Math.random());
            httpSession.setAttribute("aaaa", x); //写入随机用户名
        }
        List<foodmenu> menuList = new ArrayList<>();
        for (Iterator<foodmenu> it = menuRepository.findAll().iterator(); it.hasNext(); ) {
            menuList.add(it.next());
        }
        model.addAttribute("dishurl",dishUrl);
        model.addAttribute("menuHtml", menuList);
        return "buyer/BuyerMenuList";
    }

    @RequestMapping("/order")  //买家订单
    protected String order(Model model){
        //--------------------------加载订单--------------------------------------------------
        model.addAttribute("dishurl",dishUrl);
        String username = (String)httpSession.getAttribute("aaaa");
        List<shoppingorder> shoppingorderList = new ArrayList<>();
        for (Iterator<shoppingorder> it = orderRepository.findAllByUsername(username).iterator(); it.hasNext();)
        {
            shoppingorderList.add(it.next());
        }
        if(shoppingorderList.isEmpty()) {  //检验是否下过单
            model.addAttribute("msg","请下单后查询");
            return "/buyer/BuyerMessage";
        }
        else {
            model.addAttribute("orderHtml", shoppingorderList);
            //--------------------只存第一个数据不迭代------------------------------------------
            List<shoppingorder> shoppingorderList2 = new ArrayList<>();
            Iterator<shoppingorder> it = orderRepository.findAllByUsername(username).iterator();
            shoppingorderList2.add(it.next());

            model.addAttribute("orderHtml2", shoppingorderList2); //只取出一个时间和餐桌

            return "buyer/BuyerOrder";
        }
    }
//----------------------------------------------------------------------------------
    @RequestMapping(value = "/menuListJson")
    @ResponseBody
    protected Iterable<foodmenu>menuListJson(){
        return menuRepository.findAll();
    }
}
